﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using DailyPhoto.Common;


namespace DailyPhoto
{
    static class Logger
    {

        private static object _sync;

        private static string _logFile;


        static Logger()
        {
            _logFile = (string)Application.Config.logFile;
            _sync = new object();
        }

        public static void Write(string format, params object[] args)
        {
            lock (_sync)
            {
                using (var f = File.AppendText(_logFile))
                {
                    f.Write(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss: "));
                    f.WriteLine(string.Format(format,args));
                }
            }    
        }


        public static void Write(Exception ex)
        {
            lock (_sync)
            {
                using (var f = File.AppendText(_logFile))
                {
                    f.Write(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss: "));
                    f.WriteLine(ex.Message);
                   
                    var inner = ex;
                    while (inner != null)
                    {
                        f.WriteLine(inner.StackTrace);
                        inner = inner.InnerException;
                    }
                }
            }    
        }
    }
}
